package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    //创建持久层对象
    @Autowired
    private ClazzMapper clazzMapper;


    /**
     * 分页查询班级列表
     *
     * @param clazzQueryParam
     * @return
     */
    @Override
    public PageResult<Clazz> selectClazz(ClazzQueryParam clazzQueryParam) {

        //设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage() , clazzQueryParam.getPageSize());
        //调用查询所有的方法
        List<Clazz> clazzList = clazzMapper.selectClazz(clazzQueryParam);
        //把查询所有数据的集合强转为page对象
        Page p = (Page) clazzList;

        PageResult<Clazz> pageResult = new PageResult<>();
        pageResult.setTotal(p.getTotal());
        pageResult.setRows(p.getResult());
        return pageResult;
    }

    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    public void deleteClazz(Integer id) {
        clazzMapper.deleteClazz(id);
    }

    /**
     * 添加班级
     *
     * @param clazz
     */
    @Override
    public void insertClazz(Clazz clazz) {
        clazzMapper.insertClazz(clazz);
    }

    @Override
    public Clazz inquireById(Integer id) {
        Clazz clazz = clazzMapper.inquireById(id);
        return clazz;
    }

    /**
     * 修改部门
     *
     * @param clazz
     */
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }

    /**
     * 查询所有班级信息
     *
     * @return
     */
    @Override
    public List<Clazz> selectAll() {
        List<Clazz> clazzList = clazzMapper.selectAll();
        return clazzList;
    }
}
